{block name=body}

<div class="row">
    <div class="col-sm-12">
        <form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post">
            <div class="row">
                <div class="col-sm-12 edit-page-header">
                    <span class="edit-page-title">{if $fields.location_id neq ""}编辑{else}新建{/if} {$zone}</span> {if $cnt eq ""}{assign var=cnt value=0}{/if}
                    <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                    <div class="published" {if $fields.location_published eq 0}style="display:none;" {/if}>
                        <!-- 已发布 -->
                        <a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]',false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 保存草稿</a>
                        <a href="javascript:void(0);" onClick="unpublish('location_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
                    </div>
                    <div class="drafts" {if $fields.location_published eq 1}style="display:none;" {/if}>
                        <!-- 草稿 -->
                        <a href="javascript:void(0);" onClick="publish('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> Save &amp; 发布</a>
                    </div>
                    <input type="hidden" value="location_id" name="primary_id" id="primary_id" />
                    <input type="hidden" value="location_id" name="field[1][tbl_location][{$cnt}][id]" id="id" />
                    <input type="hidden" value="{$fields.location_id}" name="field[1][tbl_location][{$cnt}][location_id]" id="location_id" class="key">
                    <input type="hidden" value="{$rootParentID}" name="field[1][tbl_location][{$cnt}][location_listing_id]" id="location_listing_id">
                    <input type="hidden" value="{if $fields.location_object_id}{$fields.location_object_id}{else}{$objID}{/if}" name="field[1][tbl_location][{$cnt}][location_object_id]" id="location_object_id">
                    <input type="hidden" value="{if $fields.location_created}{$fields.location_created}{else}{'Y-m-d H:i:s'|date}{/if}" name="field[1][tbl_location][{$cnt}][location_created]" id="location_created">
                    <input type="hidden" value="{if $fields.location_published}1{else}0{/if}" name="field[1][tbl_location][{$cnt}][location_published]" id="location_published">
                    <input type="hidden" name="formToken" id="formToken" value="{$token}" />
                </div>
            </div>
            <ul class="nav nav-tabs" id="myTab">
                <li class="active"><a href="#details" data-toggle="tab">详情</a></li>
                <li><a href="#log" data-toggle="tab">日志</a></li>
            </ul>
            <div class="tab-content">
                <!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane active" id="details">
                    <div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_flag">新的地点</label>
                            <div class="col-sm-5">
                                <input type="hidden" value="{if $fields.location_flag eq 1}1{else}0{/if}" name="field[1][tbl_location][{$cnt}][location_flag]" class="value">
                                <input class="chckbx" type="checkbox" {if $fields.location_flag eq 1} checked="checked" {/if} onclick="if($(this).is(':checked')){ $(this).parent().children('.value').val('1'); }else{ $(this).parent().children('.value').val('0'); }" id="id_location_flag">
                            </div>
                        </div>
                         <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_name">名称 *</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_name}" name="field[1][tbl_location][{$cnt}][location_name]" id="id_location_name" required>
                                <span class="help-block"></span>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_description">描述</label>
                            <div class="col-sm-5">
                                <textarea name="field[1][tbl_location][{$cnt}][location_description]" id="id_location_description" class="tinymce">{$fields.location_description}</textarea>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="franchisee_id">加盟商</label>
                            <div class="col-sm-5">
                                <select class="form-control" name="field[1][tbl_location][{$cnt}][location_franchisee_id]" id="franchisee_id">
                                    <option value="">请选择</option>
                                    {foreach $fields.options.franchisee as $opt}
                                    <option value="{$opt.id}" {if $fields.location_franchisee_id eq $opt.id}selected="selected" {/if}>{$opt.value}</option> {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_street">地址</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_street}" name="field[1][tbl_location][{$cnt}][location_street]" id="id_location_street">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_state">省/市 *</label>
                            <div class="col-sm-5">
                                <select class="form-control required" name="field[1][tbl_location][{$cnt}][location_state]" id="id_location_state">
                                    <option value="">请选择</option>
                                    {foreach from=$fields.options.states item=state}
                                    <option value="{$state.value}" {if $fields.location_state eq $state.value}selected="selected" {/if}>{$state.value}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_suburb">城市 *</label>
                            <div class="col-sm-5">
                                <select class="form-control required" type="text" name="field[1][tbl_location][{$cnt}][location_suburb]" id="id_location_suburb">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_postcode">邮编</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_postcode}" name="field[1][tbl_location][{$cnt}][location_postcode]" id="id_location_postcode">
                            </div>
                        </div>
            <!--        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="search">地图位置</label>
                            <div class="col-sm-7">
                                <input class="form-control location-input" type="text" value="" id="search">&nbsp;
                                <a href="javascript:void(0);" class="btn btn-info" onclick="searchAddress($('#search').val());$('#search').val('')">Search</a>
                                <div id="search-warning"></div>
                                <input type="hidden" value="{$fields.location_latitude}" name="field[1][tbl_location][{$cnt}][location_latitude]" id="location_latitude">
                                <input type="hidden" value="{$fields.location_longitude}" name="field[1][tbl_location][{$cnt}][location_longitude]" id="location_longitude">
                                <div id="GmlMap" class="GmlMap">加载地图....</div>
                                <script type="text/javascript">
                                      jQuery(document).ready(function() {
                                        centerOn({$fields.location_latitude},{$fields.location_longitude});
                                      });
                                    </script>
                                <script src="//maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
                                <script type="text/javascript" src="/admin/includes/google-api/gml-v3.js"></script>
                                <link href='/admin/includes/google-api/gml-v3.css' rel='stylesheet' type='text/css'>
                            </div>
                        </div> -->
                        <div class="row form-group">
                            <label class="col-sm-3 control-label" for="id_location_url">网站
                                <br><small>包括 'http://' 或 'https://'</small></label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_url}" name="field[1][tbl_location][{$cnt}][location_url]" id="id_location_url">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label">纬度</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_latitude}" name="field[1][tbl_location][{$cnt}][location_latitude]" id="id_location_latitude">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-sm-3 control-label">经度</label>
                            <div class="col-sm-5">
                                <input class="form-control" type="text" value="{$fields.location_longitude}" name="field[1][tbl_location][{$cnt}][location_longitude]" id="id_location_longitude">
                            </div>
                        </div>
                        <!-- 					<div class="row form-group">
                                                    <label class="col-sm-3 control-label" for="id_location_phone">电话</label>
                                                    <div class="col-sm-5">
                                                        <input class="form-control" type="text" value="{$fields.location_phone}" name="field[1][tbl_location][{$cnt}][location_phone]" id="id_location_phone">
                                                    </div>
                                                </div>
                                                <div class="row form-group">
                                                    <label class="col-sm-3 control-label" for="id_location_fax">Fax</label>
                                                    <div class="col-sm-5">
                                                        <input class="form-control" type="text" value="{$fields.location_fax}" name="field[1][tbl_location][{$cnt}][location_fax]" id="id_location_fax">
                                                    </div>
                                                </div>
                                                <div class="row form-group">
                                                    <label class="col-sm-3 control-label" for="id_location_email">Email</label>
                                                    <div class="col-sm-5">
                                                        <input class="form-control" type="text" value="{$fields.location_email}" name="field[1][tbl_location][{$cnt}][location_email]" id="id_location_email">
                                                    </div>
                                                </div>
                                          <div class="row form-group">
                                      <label class="col-sm-3 control-label" for="id_location_enquiry_recipient">Enquiry Recipient</label>
                                      <div class="col-sm-5">
                                        <input class="form-control" type="text" value="{$fields.location_enquiry_recipient}" name="field[1][tbl_location][{$cnt}][location_enquiry_recipient]" id="id_location_enquiry_recipient">
                                      </div>
                                    </div>
                                    <div class="row form-group">
                                      <label class="col-sm-3 control-label" for="id_location_order_recipient">Order Recipient</label>
                                      <div class="col-sm-5">
                                        <input class="form-control" type="text" value="{$fields.location_order_recipient}" name="field[1][tbl_location][{$cnt}][location_order_recipient]" id="id_location_order_recipient">
                                      </div>
                                    </div>
                                    <div class="row form-group">
                                      <label class="col-sm-3 control-label" for="id_location_bcc_recipient">BCC Recipient</label>
                                      <div class="col-sm-5">
                                        <input class="form-control" type="text" value="{$fields.location_bcc_recipient}" name="field[1][tbl_location][{$cnt}][location_bcc_recipient]" id="id_location_bcc_recipient">
                                      </div>
                                    </div>
                                          <div class="row form-group">
                                                        <label class="col-sm-3 control-label" for="location_image">Image<br>
                                                        <small>尺寸: 350px宽 * 350px高 <br>(optional)</small></label>
                                                    <div class="col-sm-9">
                                                        <input type="hidden" value="{$fields.location_image}" name="field[1][tbl_location][{$cnt}][location_image]" id="location_image_link" class="fileinput">
                                                        <span class="file-view" id="location_image_path"> {if $fields.location_image}<a href="{$fields.location_image}" target="_blank" >查看</a>{else}暂无{/if} </span>
                                                        <a href="javascript:void(0);" class="btn btn-info marg-5r" onclick="getFileType('location_image','','');">选择文件</a>
                                                        <a href="javascript:void(0);" class="btn btn-info" onclick="$('#location_image_link').val('');$('#location_image_path').html('None');">移除文件</a>
                                                    </div>
                                                </div>
                        -->
                    </div>
                </div>

                <!--===+++===+++===+++===+++===+++ LOG TAB +++===+++===+++===+++===+++====-->
                <div class="tab-pane" id="log">
                    <div class="row form" id="tags-wrapper">
                        <div class="col-sm-12">
                            {if $fields.logs}
                                <table class="table table-bordered table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th>时间</td>
                                            <th>操作</td>
                                            <th>用户</td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    {foreach $fields.logs as $log}
                                        <tr {if $log.location_id eq $fields.location_id}class="info"{/if}>
                                            <td>{$log.log_created|date_format:"%d/%b/%Y %r"}</td>
                                            <td>{$log.log_action}{if $log.log_action eq 'Add' || $log.log_action eq 'Delete'} draft{/if}</td>
                                            <td>{$log.admin_name}</td>
                                        </tr>
                                    {/foreach}
                                    </tbody>
                                </table>
                            {else}
                                暂无日志。
                            {/if}
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="row form-group form-bottom-btns">
                <a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
                <div class="published" {if $fields.location_published eq 0}style="display:none;"{/if}>
                    <!-- 已发布 -->
                    <a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]', false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 发布草稿</a>
                    <a href="javascript:void(0);" onClick="unpublish('location_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
                </div>
                <div class="drafts" {if $fields.location_published eq 1}style="display:none;"{/if}>
                    <!-- 草稿 -->
                    <a href="javascript:void(0);" onClick="publish('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
                </div>
            </div>
        </form>
    </div>
</div>

{include file='jquery-validation.tpl'}
<script type="text/javascript">
function changeSuburb(state){
    $.ajax({
        url: "/process/regionsuburbs",
        dataType: "json",
        data: {
          "state": state
        },
        success: function( data ) {
            console.log(data);
            $("select#id_location_suburb").html("");
            for(var i=0;i<data.length;i++){   
                $("select#id_location_suburb").append("<option value='"+data[i].text+"'>"+data[i].text+"</option>");
            } 
        }
    });
}
$(document).ready(function(){
    var state = $('select#id_location_state option:selected').val();
    changeSuburb(state);
    $('#Edit_Record').validate({
        onkeyup: false
    });
    $("select#id_location_state").change(function(){
        var id = $('select#id_location_state option:selected').val();
        changeSuburb(id);
    });
});

function saveDraft(id_name,objId_name,publish_name, field_name, preview){
    if ($('#Edit_Record').valid()) {
        $('body').css('cursor', 'wait');
        $('#'+publish_name).val('0');
        var id_key0 = encodeURIComponent(id_name+'[0]');
        var id_key1 = encodeURIComponent(id_name+'[1]');
        var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
        var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
        var field_key = encodeURIComponent(field_name);
        var field_value = encodeURIComponent(mysql_now());
        $.ajax({
            type : "POST",
            url : "/admin/includes/processes/processes-record.php",
            cache: false,
            async: false,
            data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=0&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
            dataType: "html",
            success : function(data, textStatus) {
                try {
                    var obj = $.parseJSON(data);
                    if(obj.notice){ 
                        $('.key').val('');
                        $('#Edit_Record').submit();
                        $('.published').hide();
                        $('.drafts').show();
//                         buildUrl('tbl_location','location_parent_id',objId_name, preview);
                    }
                } catch (err) {}
                $('body').css('cursor', 'default');
            }
        });
        $('body').css('cursor', 'default');
    }
}

function publish(id_name,objId_name,publish_name,field_name){
    if ($('#Edit_Record').valid()) {
        $('body').css('cursor', 'wait');
        $('#'+publish_name).val('1');
        var id_key0 = encodeURIComponent(id_name+'[0]');
        var id_key1 = encodeURIComponent(id_name+'[1]');
        var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
        var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
        var field_key = encodeURIComponent(field_name);
        var field_value = encodeURIComponent(mysql_now());
        $.ajax({
            type : "POST",
            url : "/admin/includes/processes/processes-record.php",
            cache: false,
            data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=1&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
            dataType: "html",
            success : function(data, textStatus) {
                try {
                    var obj = $.parseJSON(data);
                    if(obj.notice){

                        $('#Edit_Record').submit();
                        $('.drafts').hide();
                        $('.published').show();
                    }
                } catch (err) {}
                $('body').css('cursor', 'default');
            }
        });
        $('body').css('cursor', 'default');
    }
}

function unpublish(publish_name){
    $('#'+publish_name).val('0');
    $('#Edit_Record').submit();
    $('.published').hide();
    $('.drafts').show();
}


//function fillLocation(){
//    var loc_str = $('#id_location_street').val() + ', ' + $('#id_location_suburb').val() + ', ' + $('#id_location_state option:selected').val();
//    $('#search').val(loc_str);
//}


$("#id_location_street").on("keyup", changeLocation);

$("#id_location_suburb").on("change", changeLocation);

$("#id_location_state").on("change", changeLocation);

function changeLocation() {
    var location_suburb = $('#id_location_suburb').val();
    var location_street  = $("#id_location_street").val();
    var location_state = $('#id_location_state option:selected').val();
    if (location_suburb != "" && location_street != "" && location_state!= "") {
        $.ajax({
            url: "/admin/includes/processes/update-locations-coordinates.php",
            type: 'POST',
            dataType:"json",
            data: {
                'location_suburb': location_suburb,
                'location_street' : location_street,
                'location_state':location_state,
            },
            success: function (data) {
                $('#id_location_latitude').val((data.lng+"").slice(0,12));
                $('#id_location_longitude').val((data.lat+"").slice(0,12));
            }
        });
    } else {
        $('#id_location_latitude').val("");
        $('#id_location_longitude').val("");
    }

};
</script>
{/block}
